'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
exports.main = async (event, context) => {
	//event为客户端上传的参数
	const {
		authorId,
		userId
	} = event;

	const user = await db.collection("user").doc(userId).get();
	const authorListIds = user.data[0].author_likes_ids;

	let msg = '';
	let author_ids = null;

	//判断关注状态
	if (authorListIds.includes(authorId)) {
		//取消关注
		author_ids = dbCmd.pull(authorId)
		msg = "取消关注"

	} else {
		//关注
		author_ids = dbCmd.addToSet(authorId)
		msg = "关注成功"
	}

	await db.collection("user").doc(userId).update({
		author_likes_ids: author_ids
	})

	//返回数据给客户端
	return {
		code: 0,
		data: {
			msg,
		}
	}
};
